//#define __DEBUG__
#define THIS_FILE "x_prio_1.sqf"
#include "x_setup.sqf"

__TRACE_1("_this","_this")

private ["_poss","_kind","_objs","_unit","_svec","_isFlat","_crew","_pos","_center_x","_center_y","_count_obj","_radius","_angle","_dir","_angle_plus","_bag"];
PARAMS_2(_poss,_kind);
__GetEGrp(_newgroup)
_objs = [];
_unit = createVehicle [GVAR(sm_arty), _poss, [], 0, "NONE"];
_svec = (sizeOf (typeOf _unit) max 1);
_isFlat = (position _unit) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _unit];
if (count _isFlat > 1) then {
	_isFlat set [2,0];
	if ((_poss distance _isFlat) < 50) then {
		_poss = _isFlat;
	};
};
_poss set[2,0];
_unit setPos _poss;
_crew = [_unit, _newgroup,false,"",GVAR(crew_override_E)] call FUNC(spawnCrew);
_unit setPos _poss;
_unit lock 3;

_pos = (_unit modelToWorld [0,_svec,0]);
_pos set [2,0];
_bag = "Land_BagFence_Round_F" createVehicle _pos;
_bag setDir 180;
_objs set [count _objs, _bag];
_pos = (_unit modelToWorld [0,(0 - _svec),0]);
_pos set [2,0];
_bag = "Land_BagFence_Round_F" createVehicle _pos;
_bag setDir 0;
_objs set [count _objs, _bag];

_center_x = (getPos _unit) select 0;
_center_y = (getPos _unit) select 1;
_radius = (5 max (_svec + 2));
_angle = 45;
_count_obj = 4;
_angle_plus = 360 / _count_obj;
for "_i" from 0 to (_count_obj - 1) do {
	_x1 = _center_x - (_radius * sin _angle);
	_y1 = _center_y - (_radius * cos _angle);
	_pos = [_x1,_y1,0];
	_dir = (_angle + 180);
	_angle = _angle + _angle_plus;
	_bag = createVehicle ["Land_HBarrierWall6_F",_pos, [], 0, "CAN_COLLIDE"];
	_bag setDir _dir;
	_bag setPos _pos;
	_objs set [count _objs, _bag];
};

_objs = [_objs, _crew] call FUNC(arrayPushStack);

_unit spawn {
	private ["_arty","_players","_center","_radius","_target_array2","_target","_ammos","_man_type"];
	_arty = _this;
	__TargetInfo
	_center = (_target_array2 select 0);
	_radius = ((_target_array2 select 2) * 1.7);
	_ammos = getArtilleryAmmo [_arty];
	
	_man_type = switch (GVAR(enemy_side)) do {
		case "BLUFOR": {"SoldierWB"};
		case "OPFOR": {"SoldierEB"};
		case "INDEPENDENT": {"SoldierGB"};
	};
	
	if (isMultiPlayer) then { sleep (200 + random (100)); };
	while { alive _arty && canFire _arty } do {
		call FUNC(mpcheck);
		_players = if (isMultiPlayer) then { playableUnits } else { [player] };
		for "_i" from 0 to (count _players - 1) do {
			if ((GV((_players select _i),xr_pluncon)) || {((_players select _i) distance _center) > _radius} || {((_players select _i) distance _arty) < 100} || {(count ((_players select _i) nearEntities [_man_type, 30]) > 0)}) then {
				_players set [_i, objNull];
			};
		};
		_players = _players - [objNull];
		__TRACE_1("Found targets","_players")
		if (count _players > 0) then {
			_target = _players call FUNC(randomArrayVal);
			__TRACE_2("Fire","_target","_ammos")
			_arty doArtilleryFire [getPos _target, (_ammos select 0), 2];
			_arty setVehicleAmmo 1;
		};
		sleep (160 + random (100))
	};
};

__TRACE_2("End","_unit","_objs");
[_unit,_objs]